package com.ext.aic.box;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.text.TextUtils;

import com.ext.aic.R;
import com.ext.aic.box.enums.Packages;
import com.ext.aic.builder.ForegroundServiceBuilder;
import com.ext.aic.handler.ForegroundServiceHandler;
import com.ext.aic.handler.NotificationHandler;
import com.ext.aic.utils.ToastyUtils;

public class RegisterNotificationService extends NotificationListenerService {

    private ForegroundServiceHandler foregroundServiceHandler = new ForegroundServiceHandler();
    private NotificationHandler notificationHandler = new NotificationHandler();

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        return foregroundServiceHandler.onStartCommand();
    }

    @SuppressLint({"ForegroundServiceType", "InlinedApi"})
    @Override
    public void onCreate() {
        ForegroundServiceBuilder builder = new ForegroundServiceBuilder()
                .setNotificationId(1884)
                .setChannelId("NotificationListenerChannel")
                .setForegroundNotifyTitle(getString(R.string.server_running))
                .setForegroundNotifyDesc(getString(R.string.server_background_running))
                .setChannelName(getString(R.string.channel_title))
                .setChannelDescription(getString(R.string.channel_description));
        foregroundServiceHandler.onCreate(this, builder);
    }

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        String packageName = sbn.getPackageName();
        if (TextUtils.equals(packageName, Packages.WECHAT.getPackageName())) {
            if (ReplyHostingTask.isAllowReply()) {
                //这里实现接收通知消息，并尝试部分场景后台回复
            } else {
                ToastyUtils.getInstance().show(getString(R.string.the_hosting_time_has_been_set_and_cannot_be_returned));
            }
        }
    }
}
